package com.mj.mafeed.activity;

import com.mj.mafeed.R;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * 底部导航
 * @author zhaominglei
 * @date 2015-11-25
 * 
 */
public class MainActivity extends TabActivity implements OnCheckedChangeListener{
	
	private TabHost tabHost;
	private Intent indexIntent;
	private Intent productIntent;
	private Intent forumTopicIntent;
	private Intent cartIntent;
	private Intent memberIntent;
	private RadioButton tabIndexBtn;
	private RadioButton tabProductBtn;
	private RadioButton tabForumTopicBtn;
	private RadioButton tabCartBtn;
	private RadioButton tabMemberBtn;
	private String referer; //来自那个链接
	private String gowhere; //跳到那里
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        tabHost = getTabHost();
        init();
    }

	private void init() {
		tabIndexBtn = ((RadioButton) findViewById(R.id.tab_index));
        tabProductBtn = ((RadioButton) findViewById(R.id.tab_allproduct));
        tabForumTopicBtn = ((RadioButton) findViewById(R.id.tab_news));
        tabCartBtn = ((RadioButton) findViewById(R.id.tab_cart));
        tabMemberBtn = ((RadioButton) findViewById(R.id.tab_usercenter));
        
        tabIndexBtn.setOnCheckedChangeListener(this);
        tabProductBtn.setOnCheckedChangeListener(this);
        tabForumTopicBtn.setOnCheckedChangeListener(this);
        tabCartBtn.setOnCheckedChangeListener(this);
        tabMemberBtn.setOnCheckedChangeListener(this);
        
        indexIntent = new Intent(this, IndexActivity.class);
        productIntent = new Intent(this, ProductActivity.class);
        forumTopicIntent = new Intent(this, ForumTopicActivity.class);
        cartIntent = new Intent(this, CartActivity.class);
        memberIntent = new Intent(this, MemberActivity.class);
        referer = getIntent().getStringExtra("referer");
        gowhere = getIntent().getStringExtra("gowhere");
        if (referer != null && referer.equals("LoginActivity")) {
        	memberIntent.putExtra("referer", "LoginActivity");
			int member_login_success = getIntent().getIntExtra("member_login_success", 0);
			memberIntent.putExtra("member_login_success", member_login_success);
		} else if (referer != null && referer.equals("RegisterActivity")) {
			memberIntent.putExtra("referer", "RegisterActivity");
			int member_register_success = getIntent().getIntExtra("member_register_success", 0);
			memberIntent.putExtra("member_register_success", member_register_success);
		}
        
        setupIntent();
        if (referer != null && referer.equals("LoginActivity")) {
        	tabMemberBtn.setChecked(true);
        } else if (referer != null && referer.equals("RegisterActivity")) {
        	tabMemberBtn.setChecked(true);
        } else if (referer != null && referer.equals("SettingActivity")) {
        	tabMemberBtn.setChecked(true);
        } else if (referer != null && referer.equals("CartActivity")) {
        	if (gowhere != null && gowhere.equals("goshopping")) {
        		tabProductBtn.setChecked(true);
			} else {
				tabCartBtn.setChecked(true);
			}
        } else if (referer != null && referer.equals("CheckoutActivity")) {
        	tabCartBtn.setChecked(true);
        } else if (referer != null && referer.equals("PayActivity")) {
        	tabCartBtn.setChecked(true);
        } else if (referer != null && referer.equals("ForumTopicActivity")) {
        	tabForumTopicBtn.setChecked(true);
        } else if (referer != null && referer.equals("ForumTopicDetailActivity")) {
        	tabForumTopicBtn.setChecked(true);
        } else {
        	tabIndexBtn.setChecked(true);
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			switch (buttonView.getId()) {
			case R.id.tab_index:
				tabHost.setCurrentTabByTag("tab_index");
				break;
			case R.id.tab_allproduct:
				tabHost.setCurrentTabByTag("tab_allproduct");
				break;
			case R.id.tab_news:
				tabHost.setCurrentTabByTag("tab_news");
				break;
			case R.id.tab_cart:
				tabHost.setCurrentTabByTag("tab_cart");
				break;
			case R.id.tab_usercenter:
				tabHost.setCurrentTabByTag("tab_usercenter");
				break;
			}
		}
	}
	
	private void setupIntent() {
		tabHost.addTab(buildTabSpec("tab_index", R.string.tab_index, indexIntent));
		tabHost.addTab(buildTabSpec("tab_allproduct", R.string.tab_allproduct, productIntent));
		tabHost.addTab(buildTabSpec("tab_news", R.string.tab_news, forumTopicIntent));
		tabHost.addTab(buildTabSpec("tab_cart", R.string.tab_cart, cartIntent));
		tabHost.addTab(buildTabSpec("tab_usercenter", R.string.tab_usercenter, memberIntent));
	}
	
	@SuppressWarnings("unused")
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return tabHost.newTabSpec(tag).setIndicator(getString(resLabel),
				getResources().getDrawable(resIcon)).setContent(content);
	}
	
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, final Intent content) {
		return tabHost.newTabSpec(tag).setIndicator(getString(resLabel)).setContent(content);
	}
}
